// pages/news/newsList/newsList.js
const app = getApp();
//解析html
const WxParse = require('../../../wxParse/wxParse.js');
import Api from '../../../utils/http';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    newsList: [],
    searchFlag: true,
    input:'',//搜索关键
    page: 1, //页码
    pageSize: 9, //每页显示条数
    types:null,//类型
  },
  //跳转 搜索关键字
  toSearchKeyWord: function () {
    //点击输入框
    this.setData({
      searchFlag: false
    })
  },
//获取输入关键字
input:function(e){
  var that = this;
  that.setData({
    input: e.detail.value
  });
  this.getSearch();
},
  //跳转新闻详情
  toNewsDetails: function (e) {
    var that=this;
    var  {newsList} = that.data;
    let index = e.currentTarget.dataset.index;
    console.log(e.currentTarget.dataset);
    var lid = newsList[index].id;
    var type=that.data.types;
    console.log(type);
    // var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/news/newsDetails/newsDetails?lid=${lid}&type=${type}`,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    var type = options.type;
    if (type == 1) {
      wx.setNavigationBarTitle({
        title: '新闻资讯'
      })
      that.setData({
        types:1
      })
    } else if (type == 4) {
      wx.setNavigationBarTitle({
        title: '小知识'
      })
      that.setData({
        types:4
      })
    }
    this.setData({
      options:options
    })
    this.getNews();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.getNews();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.refreshData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.getNews();
  },
  /**下拉刷新*/
  refreshData() {
    var that = this;
    var newsList=that.data.newsList;
    var param = {
      type: 1,
      pageNum: 1, // 页码默认为0
      pageSize: 6 // 每页显示合数，默认为10
    }
    Api.ajax('/app/Zarticle/articleList', param).then(res => {
      console.log(res)
      
      that.setData({
        newsList: res.data,
        page: 2 // 一定要记得重置页码 ---- 没有数据的提示（上拉加载提示过后）
      })
      // 真机测试的时候，下拉刷新技术需要停止 下拉刷新的操作
      wx.stopPullDownRefresh();
    }).catch((err) => {
      console.log(err)
    })
  },
  //搜索
  getSearch: function(e) {
    var that = this;
    var type=that.data.options.type;
    var page = that.data.page;
    var param = {
      type: type,
      pageNum: 1,
      pageSize: that.data.pageSize,
      keyWord:that.data.input
    }
    console.log(param)
    Api.ajax("/app/Zarticle/articleList", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        that.setData({
          newsList:res.data,
        });
      }
    })
  },
  //新闻、小知识
  getNews: function() {
    var that = this;
    var newslist = that.data.newsList;
    var type=that.data.options.type;
    var page = that.data.page;
    var param = {
      type: type,
      pageNum: page,
      pageSize: that.data.pageSize,
    }
    console.log(param)
    Api.ajax("/app/Zarticle/articleList", param).then(res => {
      console.log(res)
      if (res.code == 200) {
        page++;
        that.setData({
          newsList: newslist.concat(res.data),
          page: page,
        });
        
        
      }else{
        if(page == 1 ){
          wx.showToast({
            icon: 'none',
            title: '暂时没有数据哦!',
          })
        }else{
          wx.showToast({
            icon: 'none',
            title: '没有更多了!',
          })
        }
        
      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})